# -*- coding: utf-8 -*-
# @Time    : 2019-12-16 19:39
# @Author  : Yang Feng
# @Email   : yangfeng@bbdservice.com
# @File    : R28_IC_AccountStatusCheck_WHB_2D.py
# @Software: PyCharm


'''eof
name:申请人贷款、贷记卡和准贷记卡账户状态校验
code:R28_IC_AccountStatusCheck_WHB_2D
tableName:
columnName:
groups:弱规则>个人征信校验
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif table == "":
            return ""
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return ""
        elif table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False



def R28_IC_AccountStatusCheck_WHB_2D():
    factor1 = factor2 = factor3 = factor4 = factor5 = factor6 = factor7 = factor8 = []
    data = IC_DSJ_2D['data']
    PDA = getTable(data, 'PDA', 1)
    if PDA is None:
        return u'缺失值'
    elif PDA == "":
        return 0
    else:
        # PDA.信贷交易信息明细（PD01）
        PD01 = getTable(PDA[0], 'PD01', 0)
        if PD01 is None or PD01 == "":
            return u'缺失值'
        elif PD01 == [{}]:
            return 0
        else:
            for PD01_Items in PD01:
                try:
                    # 基本信息段(PD01A)
                    PD01A = getTable(PD01_Items, 'PD01A', 1)
                    # 最新表现信息段(PD01B)
                    PD01B = getTable(PD01_Items, 'PD01B', 1)
                    # 最近一次月度表现信息段（PD01C）
                    PD01C = getTable(PD01_Items, 'PD01C', 0)
                    if PD01A is None or PD01A == "":
                        return u'缺失值'
                    else:
                        # 账户类型(PD01AD01）
                        PD01AD01 = PD01A[0].get('PD01AD01')
                        if PD01B is None or PD01B == "":
                            factor1.append(None)
                            factor2.append(None)
                            factor3.append(None)
                            factor4.append(None)
                        else:
                            # 账户状态(PD01BD01)
                            PD01BD01 = PD01B[0].get('PD01BD01')
                            if PD01AD01 in null_type_list or PD01BD01 in null_type_list:
                                factor1.append(None)
                                factor2.append(None)
                                factor3.append(None)
                                factor4.append(None)
                            else:
                                # 账户类型(PD01AD01）为【D1非循环贷账户】,且.业务种类（以上列明所有业务种类）,且账户状态(PD01BD01)为【2-逾期，4-呆账，6-担保物不足，7-强制平仓，8-司法追偿】的笔数
                                if PD01AD01 == 'D1' and PD01BD01 in ['2', '4', '6', '7', '8']:
                                    factor1.append(1)
                                else:
                                    factor1.append(0)
                                # 账户类型(PD01AD01）为【R1循环贷账户】,且最新表现信息段(PD01B).业务种类（以上列明所有业务种类）,且账户状态(PD01BD01)为【2-逾期，4-呆账，5-银行止付，6-担保物不足，8-司法追偿】的笔数；
                                if PD01AD01 == 'R1' and PD01BD01 in ['2', '4', '5', '6', '8']:
                                    factor2.append(1)
                                else:
                                    factor2.append(0)
                                # 账户类型(PD01AD01）为【R4循环额度下分账户】,且最新表现信息段(PD01B).业务种类（以上列明所有业务种类）,且账户状态(PD01BD01)为【2-逾期，4-呆账，6-担保物不足，8-司法追偿】的笔数；
                                if PD01AD01 == 'R4' and PD01BD01 in ['2', '4',  '6', '8']:
                                    factor3.append(1)
                                else:
                                    factor3.append(0)
                                # 账户类型(PD01AD01）为【R2贷记卡,R3准贷记卡】,且最新表现信息段(PD01B).业务种类（以上列明所有业务种类）,且账户状态(PD01BD01)为【2-冻结，5-呆账，31-银行止付, 8-司法追偿】的笔数；
                                if PD01AD01 in ['R2', 'R3'] and PD01BD01 in ['2', '5',  '31', '8']:
                                    factor4.append(1)
                                else:
                                    factor4.append(0)

                            if PD01C is None or PD01C == "":
                                factor5.append(None)
                                factor6.append(None)
                                factor7.append(None)
                                factor8.append(None)
                            elif PD01C == [{}]:
                                factor5.append(0)
                                factor6.append(0)
                                factor7.append(0)
                                factor8.append(0)
                            else:
                                # 账户状态(PD01AD01）
                                PD01CD01 = PD01A[0].get('PD01CD01')
                                if PD01AD01 in null_type_list or PD01CD01 in null_type_list:
                                    factor5.append(None)
                                    factor6.append(None)
                                    factor7.append(None)
                                    factor8.append(None)
                                else:
                                    # 账户类型(PD01AD01）为【D1非循环贷账户】,且最近一次月度表现信息段(PD01C)账户状态(PD01CD01)为【2-逾期，4-呆账，6-担保物不足，7-强制平仓，8-司法追偿】的笔数；
                                    if PD01AD01 == 'D1' and PD01CD01 in ['2', '4', '6', '7', '8']:
                                        factor5.append(1)
                                    else:
                                        factor5.append(0)

                                    # 账户类型(PD01AD01）为【R1循环贷账户】,且最近一次月度表现信息段(PD01C)账户状态(PD01CD01)为【2-逾期，4-呆账，5-银行止付，6-担保物不足，8-司法追偿】的笔数；
                                    if PD01AD01 == 'R1' and PD01CD01 in ['2', '4', '5', '6', '8']:
                                        factor6.append(1)
                                    else:
                                        factor6.append(0)

                                    # 账户类型(PD01AD01）为【R4循环额度下分账户】,且最近一次月度表现信息段(PD01C)账户状态(PD01CD01)为【2-逾期，4-呆账，6-担保物不足，8-司法追偿】的笔数；
                                    if PD01AD01 == 'R4' and PD01CD01 in ['2', '4', '6', '8']:
                                        factor7.append(1)
                                    else:
                                        factor7.append(0)

                                    # 基本信息段(PD01A).账户类型(PD01AD01）为【R2贷记卡,R3准贷记卡】,且最近一次月度表现信息段(PD01C)账户状态(PD01CD01)为【2-冻结，5-呆账，31-银行止付, 8-司法追偿】的笔数；
                                    if PD01AD01 in ['R2', 'R3'] and PD01CD01 in ['2', '5', '31', '8']:
                                        factor8.append(1)
                                    else:
                                        factor8.append(0)
                except:
                    factor1.append(None)
                    factor2.append(None)
                    factor3.append(None)
                    factor4.append(None)
                    factor5.append(None)
                    factor6.append(None)
                    factor7.append(None)
                    factor8.append(None)
                    continue

    factor1NotNone = filter(lambda x: x is not None, factor1)
    factor2NotNone = filter(lambda x: x is not None, factor2)
    factor3NotNone = filter(lambda x: x is not None, factor3)
    factor4NotNone = filter(lambda x: x is not None, factor4)
    factor5NotNone = filter(lambda x: x is not None, factor5)
    factor6NotNone = filter(lambda x: x is not None, factor6)
    factor7NotNone = filter(lambda x: x is not None, factor7)
    factor8NotNone = filter(lambda x: x is not None, factor8)
    if len(factor1NotNone)==len(factor2NotNone)==len(factor3NotNone)==len(factor4NotNone)==len(factor5NotNone)==len(factor6NotNone)==len(factor7NotNone)==len(factor8NotNone) == 0:
        return u'缺失值'
    else:
        return sum(factor1NotNone+factor2NotNone+factor3NotNone+factor4NotNone+factor5NotNone+factor6NotNone+factor7NotNone+factor8NotNone)


result = R28_IC_AccountStatusCheck_WHB_2D()